package com.ibeeking.found.open.api.feign.fallback;

import com.ibeeking.found.open.api.common.query.FileNameQuery;
import com.ibeeking.found.open.api.feign.OpenFileFeignClient;
import com.ibeeking.nematos.file.oss.dto.OssPolicyDto;
import com.ibeeking.nematos.utils.result.ResponseCodeEnum;
import com.ibeeking.nematos.utils.result.ResponseResult;
import feign.Response;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


/**
 * 降级工场
 *
 * @Author ibeeking
 * @date 2019/1/18
 */
@Component
public class OpenFileFeignFallbackFactory implements FallbackFactory<OpenFileFeignClient> {
    @Override
    public OpenFileFeignClient create(Throwable throwable) {
        return new OpenFileFeignClient() {
            @Override
            public ResponseResult<OssPolicyDto> policy() {
                return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), "进入降级工厂，文件上传失败。" + throwable.getMessage());
            }
            @Override
            public Response queryFile(FileNameQuery query) {
                return null;
            }
            @Override
            public ResponseResult<String> downloadFile(FileNameQuery query) {
                return null;
            }
            @Override
            public ResponseResult<String> uploadFile(MultipartFile file) throws IOException {
                return ResponseResult.fail(ResponseCodeEnum.SYSTEM_ERROR.getCode(), "进入降级工厂，文件上传失败。" + throwable.getMessage());
            }
        };
    }
}
